var piecolors = ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'];
$.ajax({
    url: "/saas/statistics_chain/?ishome=1&ENTSTATUS=1",
    type: "POST",
    success: function (data) {
        indexchart(data);
    }
});
var opt2com = [], opt2person = [], opt4com = [], opt4person = [],
    fig_container2, fig_container4,
    opt2, opt41, opt42,
    endmax = 1, maxcom = 1, maxperson = 1, maplegendtext = "在营企业";
var startchart = function () {
    fig_container2 = echarts.init(document.getElementById('fig_container2'));
    opt2 = {
        color: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9', '#f15c80', '#e4d354', '#8085e8', '#8d4653', '#91e8e1'],
        title: {
            x: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data: ["在营企业", "在营个体"],
            selected: {
                '在营企业': false,
                '在营个体': false
            },
            selectedMode: "single"
        },
        visualMap: {
            min: 0,
            max: endmax,
            show: true,
            x: 'left',
            y: 'bottom',
            text: ['多', '少'],
            calculable: true,
            itemWidth: 10,
            itemHeight: 60,
            inRange: {
                color: ['#BBDEFB', '#42A5F5', '#0352B2'],
                symbolSize: [30, 100]
            }
        },
        toolbox: {
            show: false
        },
        map: {
            mapUnSelect: {}
        },
        series: [{
            name: "在营企业",
            type: 'map',
            zoom: 1.2,
            label: {
                normal: {
                    show: true
                },
                emphasis: {
                    show: true
                }
            },
            itemStyle: {
                normal: {
                    show: true
                },
                emphasis: {
                    borderWidth: 1,
                    borderColor: "#ddd",
                    areaColor: "",
                    opacity: 0.8
                }
            },
            mapType: "china",
            roam: true,
            scaleLimit: {min: 1},
            data: opt2com
        }, {
            name: "在营个体",
            type: 'map',
            zoom: 1.2,
            label: {
                normal: {
                    show: true
                },
                emphasis: {
                    show: true
                }
            },
            itemStyle: {
                normal: {
                    show: true
                },
                emphasis: {
                    borderWidth: 1,
                    borderColor: "#ddd",
                    areaColor: "",
                    opacity: 0.8
                }
            },
            mapType: "china",
            roam: true,
            scaleLimit: {min: 1},
            data: opt2person
        }]
    };
    fig_container2.on("legendselectchanged", function (legend) {
        maplegendtext = legend.name;
        if (maplegendtext.indexOf("企业") > -1) {
            endmax = maxcom;
        } else {
            endmax = maxperson;
        }
        if (endmax == 0) {
            endmax = 1;
        }
        fig_container2.setOption({visualMap: {max: endmax}});
    });
    opt2.legend.selected[maplegendtext] = true;
    fig_container2.setOption(opt2, true);
    fig_container4 = echarts.init(document.getElementById('fig_container4'));
    opt41 = {
        title: {
            text: ""
        },
        color: piecolors,
        tooltip: {trigger: "item", formatter: "{a} <br/>{b} : {c} ({d}%)"},
        series: [{
            name: "行业",
            type: "pie",
            radius: "60%",
            center: ['55%', '50%'],
            data: opt4com
        }]
    };
    opt42 = {
        title: {
            text: ""
        },
        color: piecolors,
        tooltip: {trigger: "item", formatter: "{a} <br/>{b} : {c} ({d}%)"},
        series: [{
            name: "行业",
            type: "pie",
            radius: "60%",
            center: ['55%', '50%'],
            data: opt4person
        }]
    };
    var text = $("#pietype .inactive").text();
    if (text == "个体户") {
        fig_container4.setOption(opt41);
    }
    else {
        fig_container4.setOption(opt42);
    }
    var colors = ['#FE4639', '#FE6F1A', '#CDD313', '#05DB88', '#019fe8'];
    var fig_container5 = new Highcharts.Chart({
        chart: {
            renderTo: 'fig_container5',
            type: 'area'
        },
        title: {
            text: ""
        },
        xAxis: {
            categories: [
                "北京市", "天津市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海市", "江苏省", "浙江省", "安徽省",
                "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "重庆市", "四川省",
                "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区"]
        },
        yAxis: {
            minorGridLineColor: '#E0E0E0',
            tickInterval: '200',
            title: {
                text: ''
            }
        },
        tooltip: {
            pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {point.y}</b>'
        },
        plotOptions: {
            area: {
                marker: {
                    enabled: false,
                    symbol: 'circle',
                    radius: 2,
                    states: {
                        hover: {
                            enabled: true
                        }
                    }
                }
            }
        },
        legend: {
            enabled: false
        },
        series: [{
            name: 2016 + "年" + '企业信用地区分布',
            data: [
                435, 441, 415, 384, 429, 437, 420, 416, 440, 441, 420, 426, 428, 513, 438, 429, 426, 424, 443, 422, 418, 399, 436, 432, 431, 411, 417, 396, 427, 431, 388
            ]
        }]
    });
    var fig_container6 = new Highcharts.Chart({
        chart: {
            renderTo: 'fig_container6',
            type: 'area'
        },
        title: {
            text: ""
        },
        xAxis: {
            categories: [
                "农、林、牧、渔业", "采矿业", "制造业", "电力、热力、燃气及水生产和供应业", "建筑业",
                "批发和零售业", "交通运输、仓储和邮政业", "住宿和餐饮业", "信息传输、软件和信息技术服务业", "金融业",
                "房地产业", "租赁和商务服务业", "科学研究和技术服务业",
                "水利、环境和公共设施管理业", "居民服务、修理和其他服务业", "教育", "卫生和社会工作",
                "文化、体育和娱乐业", "公共管理、社会保障和社会组织", "国际组织"]
        },
        yAxis: {
            minorGridLineColor: '#E0E0E0',
            tickInterval: '200',
            title: {
                text: ''
            }
        },
        tooltip: {
            pointFormat: '<span style="color:{point.color}">\u25CF</span><b> {point.y}</b>'
        },
        plotOptions: {
            area: {
                marker: {
                    enabled: false,
                    symbol: 'circle',
                    radius: 2,
                    states: {
                        hover: {
                            enabled: true
                        }
                    }
                }
            }
        },
        legend: {
            enabled: false
        },
        series: [{
            name: 2016 + "年" + '行业信用评分走势',
            data: [402, 413, 433, 419, 418, 432, 430, 421, 427, 409, 421, 426, 436, 421, 420, 418, 433, 425, 409, 409]
        }]
    });
};
function indexchart(alldata) {
    var data2 = alldata["regionbar"],
        data4 = alldata["nicbar"],
        region = data2["title"],
        com = data2["企业"],
        person = data2["个体户"],
        nic = data4["title"],
        niccom = data4["企业"],
        nicperson = data4["个体户"],
        niccomsum = niccom.sum(),
        nicpersonsum = nicperson.sum();
    if (data2) {
        for (var i = 0, len = region.length; i < len; i++) {
            var obj1 = {name: region[i], value: com[i]};
            var obj2 = {name: region[i], value: person[i]};
            opt2com.push(obj1);
            opt2person.push(obj2);
        }
        maxcom = parseFloat(count(com).Q3);
        maxperson = parseFloat(count(person).Q3);
        endmax = maxcom;
        if (endmax == 0) {
            endmax = 1;
        }
    }
    if (data4) {
        for (var j = 0, lenj = nic.length; j < lenj; j++) {
            var obj41 = {
                name: nic[j], value: niccom[j], itemStyle: {
                    normal: {
                        label: {
                            show: function () {
                                var num = niccom[j] / niccomsum;
                                if (num < 0.01) {
                                    return false;
                                }
                            }()
                        },
                        labelLine: {
                            show: function () {
                                var num = niccom[j] / niccomsum;
                                if (num < 0.01) {
                                    return false;
                                }
                            }()
                        }
                    }, emphasis: {
                        label: {
                            show: true
                        },
                        labelLine: {
                            show: true
                        }
                    }
                }
            };
            var obj42 = {
                name: nic[j], value: nicperson[j], itemStyle: {
                    normal: {
                        label: {
                            show: function () {
                                var num = nicperson[j] / nicpersonsum;
                                if (num < 0.01) {
                                    return false;
                                }
                            }()
                        },
                        labelLine: {
                            show: function () {
                                var num = nicperson[j] / nicpersonsum;
                                if (num < 0.01) {
                                    return false;
                                }
                            }()
                        }
                    }, emphasis: {
                        label: {
                            show: true
                        },
                        labelLine: {
                            show: true
                        }
                    }
                }
            };
            opt4com.push(obj41);
            opt4person.push(obj42);
        }
    }
    startchart();
}
function cpie(name, _this) {
    if ($(_this).hasClass("inactive")) {
        $("#pietype .ptype").addClass("inactive");
        $(_this).removeClass("inactive");
        if (name == "com") {
            fig_container4.setOption(opt41, false);
        } else {
            fig_container4.setOption(opt42, false);
        }
    }
}

window.onresize = function () {
    startchart();
    barscore();
};
$("#fullscreenanniu").click(function () {
    startchart();
    barscore();
});


